package dp;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class partition {
    List<List<String>> result = new ArrayList<>();
    LinkedList<String> path = new LinkedList<>();
    public List<List<String>> partition(String s) {
        backtrack(s, 0, new StringBuilder());
        return result;
    }
    public boolean isPartition(String s){
        int start = 0;
        int end = s.length() - 1;
        while (start <= end){
            if (s.charAt(start) == s.charAt(end)){
                start++;
                end--;
            } else {
                return false;
            }
        }
        return true;
    }
    public void backtrack(String s, int start, StringBuilder sb){
        if (start == s.length()){
            result.add(new ArrayList<>(path));
        }
        for (int i = start; i < s.length(); i++) {
            sb.append(s.charAt(i));
            if (isPartition(sb.toString())){
                path.add(sb.toString());
                backtrack(s, start + 1, new StringBuilder());
                path.removeLast();
            }
        }
    }
}
